package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.AddPlanTaskEventReq;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.AddPlanTaskEventResp;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.HandleTaskReq;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.HandleTaskResp;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.PlanTaskApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetDetailReq;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetDetailResp;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetListReq;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetListResp;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskListReq;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskListResp;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.UploadWorkOrderByTaskAssetReq;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.UploadWorkOrderByTaskAssetResp;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetDto;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetFileDto;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetItemResultDto;
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskEventDto;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/3/23
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class PlanTaskApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final PlanTaskApiService INSTANCE = new PlanTaskApiService();
    }

    public static PlanTaskApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private PlanTaskApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private PlanTaskApiGrpc.PlanTaskApiBlockingStub getStub(ManagedChannel channel) {
        return PlanTaskApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 移动端计划列表
     * 计划类型：1-巡检计划，2-保养计划
     * 状态：1-已完成，2-未完成
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryPlanTaskListResp> queryPlanTaskList(
            ComQuery comQuery,
            int playType,
            int status,
            String taskCode,
            long handleTimeStart,
            long handleTimeEnd,
            CallBack<QueryPlanTaskListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryPlanTaskListResp>(callBack) {
            @Override
            protected QueryPlanTaskListResp doRequestData(ManagedChannel channel) {
                QueryPlanTaskListReq message = QueryPlanTaskListReq.newBuilder()
                        .setComQuery(comQuery)
                        .setPlanType(playType)
                        .setHandleStatus(status)
                        .setTaskCode(taskCode)
                        .setHandleTimeStart(handleTimeStart)
                        .setHandleTimeEnd(handleTimeEnd)
                        .build();
                Logger.d(message);
                QueryPlanTaskListResp response = null;
                try {
                    response = getStub(channel).queryPlanTaskList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 移动端任务列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryPlanTaskAssetListResp> queryPlanTaskAssetList(
            ComQuery comQuery,
            int planType,
            int status,
            String planTaskId,
            String assetName,
            String areaId,
            CallBack<QueryPlanTaskAssetListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryPlanTaskAssetListResp>(callBack) {
            @Override
            protected QueryPlanTaskAssetListResp doRequestData(ManagedChannel channel) {
                QueryPlanTaskAssetListReq message = QueryPlanTaskAssetListReq.newBuilder()
                        .setTenantNo(1)
                        .setComQuery(comQuery)
                        .setPlanType(planType)
                        .setHandleStatus(status)
                        .setAssetName(assetName)
                        .setPlanTaskId(planTaskId)
                        .setAreaId(areaId)
                        .build();
                Logger.d(message);
                QueryPlanTaskAssetListResp response = null;
                try {
                    response = getStub(channel).queryPlanTaskAssetList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryPlanTaskAssetList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }



    /**
     * 移动端任务详情
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryPlanTaskAssetDetailResp> queryPlanTaskAssetDetail(
            String planTaskId,
            String assetId,
            CallBack<QueryPlanTaskAssetDetailResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryPlanTaskAssetDetailResp>(callBack) {
            @Override
            protected QueryPlanTaskAssetDetailResp doRequestData(ManagedChannel channel) {
                QueryPlanTaskAssetDetailReq message = QueryPlanTaskAssetDetailReq.newBuilder()
                        .setTenantNo(1)
                        .setPlanTaskId(planTaskId)
                        .setAssetId(assetId)
                        .build();
                Logger.d(message);
                QueryPlanTaskAssetDetailResp response = null;
                try {
                    response = getStub(channel).queryPlanTaskAssetDetail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryPlanTaskAssetDetail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 移动端处理任务
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, HandleTaskResp> handleTask(
            PlanTaskAssetDto planTaskAssetDto,
            List<PlanTaskAssetFileDto> planTaskAssetFileDto,
            List<PlanTaskAssetItemResultDto> planTaskAssetItemResultDto,
            CallBack<HandleTaskResp> callBack) {
        return new GrpcAsyncTask<String, Void, HandleTaskResp>(callBack) {
            @Override
            protected HandleTaskResp doRequestData(ManagedChannel channel) {
                HandleTaskReq message = HandleTaskReq.newBuilder()
                        .setTenantNo(1)
                        .setPlanTaskAssetDto(planTaskAssetDto)
                        .addAllTaskAssetFileDto(planTaskAssetFileDto)
                        .addAllTaskAssetItemResultDto(planTaskAssetItemResultDto)
                        .build();
                Logger.d(message);
                HandleTaskResp response = null;
                try {
                    response = getStub(channel).handleTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "handleTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 转派
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AddPlanTaskEventResp> forwardTask(
            String planTaskId,
            String opinion,
            String assignUserName,
            String userId,
            CallBack<AddPlanTaskEventResp> callBack) {
        return new GrpcAsyncTask<String, Void, AddPlanTaskEventResp>(callBack) {
            @Override
            protected AddPlanTaskEventResp doRequestData(ManagedChannel channel) {
                AddPlanTaskEventReq message = AddPlanTaskEventReq.newBuilder()
                        .setTenantNo(1)
                        .setPlanTaskEventDto(PlanTaskEventDto.newBuilder()
                                .setPlanTaskId(planTaskId)
                                .setOpinion(opinion)
                                .setAssignUserName(assignUserName)
                                .setAssignUserId(userId)
                                .build())
                        .build();
                Logger.d(message);
                AddPlanTaskEventResp response = null;
                try {
                    response = getStub(channel).forwardTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "forwardTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 故障上报，此接口在故障工单新增后，提交新增工单id和编号用于标记此任务已上报工单
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, UploadWorkOrderByTaskAssetResp> uploadWorkOrderByTaskAsset(
            String orderId,
            String orderCode,
            String planTaskAssetId,
            CallBack<UploadWorkOrderByTaskAssetResp> callBack) {
        return new GrpcAsyncTask<String, Void, UploadWorkOrderByTaskAssetResp>(callBack) {
            @Override
            protected UploadWorkOrderByTaskAssetResp doRequestData(ManagedChannel channel) {
                UploadWorkOrderByTaskAssetReq message = UploadWorkOrderByTaskAssetReq.newBuilder()
                        .setTenantNo(1)
                        .setWorkOrderId(orderId)
                        .setOrderCode(orderCode)
                        .setPlanTaskAssetId(planTaskAssetId)
                        .build();
                Logger.d(message);
                UploadWorkOrderByTaskAssetResp response = null;
                try {
                    response = getStub(channel).uploadWorkOrderByTaskAsset(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "uploadWorkOrderByTaskAsset");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
